<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
             <el-form-item label="订单号：">
            <el-input v-model="listQuery.object.orderCode" class="input-width" placeholder="订单号" clearable></el-input>
          </el-form-item>
           <el-form-item label="产品">
            <el-input v-model="listQuery.object.productName" class="input-width" placeholder="产品" clearable></el-input>
          </el-form-item>
           <el-form-item label="品牌">
            <el-input v-model="listQuery.object.brandName" class="input-width" placeholder="品牌" clearable></el-input>
          </el-form-item>
          <el-form-item label="收货人">
            <el-input v-model="listQuery.object.receiver" class="input-width" placeholder="收货人" clearable></el-input>
          </el-form-item>
               <el-form-item label="手机号">
            <el-input v-model="listQuery.object.mobile" class="input-width" placeholder="手机号" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="订单号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.orderCode}}</template>
        </el-table-column>
        <el-table-column label="品牌" width="100" align="center">
          <template slot-scope="scope">{{scope.row.brandName}}</template>
        </el-table-column>
        <el-table-column label="产品" width="100" align="center">
          <template slot-scope="scope">{{scope.row.productName}}</template>
        </el-table-column>
        <el-table-column label="收货人" align="center">
          <template slot-scope="scope">{{scope.row.receiver}}</template>
        </el-table-column>
        <el-table-column label="手机" align="center">
          <template slot-scope="scope">{{scope.row.mobile}}</template>
        </el-table-column>
        <el-table-column label="收货地址" align="center">
          <template slot-scope="scope">{{scope.row.address}}</template>
        </el-table-column>
           <el-table-column label="订单总价" width="100" align="center">
          <template slot-scope="scope">{{scope.row.priceTatol}}</template>
        </el-table-column>
          <el-table-column label="订单状态" width="100" align="center">
            
          <template  slot-scope="scope">
            <span v-if="scope.row.auditStatus==111">待转交</span>
             <span v-if="scope.row.auditStatus==1111">待厂商确认</span>
              <span v-if="scope.row.auditStatus==1110">驳回</span>
               <span v-if="scope.row.auditStatus==11111">厂商确认完成</span>
              <span v-if="scope.row.auditStatus==11110">厂商驳回</span>
          </template>

        </el-table-column>
             <el-table-column label="订单发起人" width="100" align="center">
          <template slot-scope="scope">{{scope.row.creator}}</template>
        </el-table-column>

             <el-table-column label="下单时间" width="100" align="center">
          <template slot-scope="scope">{{scope.row.createDate}}</template>
        </el-table-column>
        
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <!-- <el-button size="mini"
                       type="text"
                       @click="handleSelectRole(scope.$index, scope.row)">分配角色
            </el-button> -->
            <el-button size="mini"
                       type="text"
                       @click="querydetail(scope.$index, scope.row)">
              详情
            </el-button>
              <el-button v-if="scope.row.auditStatus===1111" size="mini"
                       type="text"
                       @click="handleComplate(scope.$index, scope.row)">待确认
            </el-button>
            <el-button v-if="scope.row.auditStatus===1111" size="mini"
                       type="text"
                       @click="handleCancel(scope.$index, scope.row)">驳回
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑用户':'添加用户'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="admin"
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item label="帐号：">
           <el-select
    v-model="admin.userId"
    filterable
    remote
    reserve-keyword
    placeholder="请输入关键词"
    :remote-method="remoteMethod"
    :loading="loading">
    <el-option
      v-for="item in options"
      :key="item.userId"
      :label="item.username"
      :value="item.userId">
    </el-option>
  </el-select>
        </el-form-item>
       
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="订单详情"
      :visible.sync="allocDialogVisible"
      width="60%">
      <div> <span>编号：{{orderdetail.orderCode}}</span></div> 
      <div> <span>产品：{{orderdetail.productName}}</span></div>
      <div> <span>品牌：{{orderdetail.brandName}}</span></div>
      <div> <span>订单总价：{{orderdetail.priceTatol}}</span></div>
      <div> <span>收货人：{{orderdetail.receiver}}</span></div>
      <div> <span>手机号码：{{orderdetail.mobile}}</span></div>
      <div> <span>收货地址：{{orderdetail.address}}</span></div>
      <div> <span>包装要求：{{orderdetail.packing}}</span></div>
      <div> <span>备注：{{orderdetail.remark}}</span></div>
      
      <div class="table-container">
      <el-table ref="adminTable"
                :data="orderdetail.orderitems"
                style="width: 100%;"
                v-loading="" border>
        <el-table-column label="订单号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.orderCode}}</template>
        </el-table-column>
        <el-table-column label="品牌" width="100" align="center">
          <template slot-scope="scope">{{scope.row.brandName}}</template>
        </el-table-column>
        <el-table-column label="产品" width="100" align="center">
          <template slot-scope="scope">{{scope.row.productName}}</template>
        </el-table-column>
       <el-table-column label="规格" align="center">
    <el-table-column v-for="item in productSpes" :key="item.spec" :label="item.specName" width="120">
            <template slot-scope="scope">{{getSpecValue(scope.row.goodsSpecList,item.specCode)}}</template>
    </el-table-column>
        {{this.priceRuleList}}
      </el-table-column>
        <el-table-column label="单价" align="center">
          <template slot-scope="scope">{{scope.row.price}}</template>
        </el-table-column>
        <el-table-column label="数量" align="center">
          <template slot-scope="scope">{{scope.row.quantity}}</template>
        </el-table-column>
           <el-table-column label="总价" width="100" align="center">
          <template slot-scope="scope">{{scope.row.total}}</template>
        </el-table-column>       
      </el-table>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {fetchList,complateOrder,cancelOrder} from '@/api/order';
  import {querySpecByProductCode} from '@/api/goods'

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
      object: { orderCode: "", username:"",receiver:"",productName:"",brandName:"",orderStatus:"",mobile:"" }
  };
  const defaultAdmin = {
    id: null,
    username: null,
    password: null,
    nickName: null,
    email: null,
    note: null,
    status: 1
  };
  export default {
    name: 'adminList',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        admin: Object.assign({}, defaultAdmin),
        isEdit: false,
        allocDialogVisible: false,
        allocRoleIds:'',
        allRoleList:[],
        allocAdminId:null,
        orderdetail:{},
        options: [],
        productSpes:[]
      }
    },
    created() {
      this.getList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.admin = Object.assign({},defaultAdmin);
      },
      getSpecValue(val, specCode) {
      if (val == null) {
        return;
      }
      var array = val.filter(item => {
        if (item.specCode === specCode) {
          return item;
        }
      });
      if (array == undefined || array.length <= 0) {
        return;
      }
      return array[0].specValue;
    },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus(row.userId, {status: row.status}).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      querydetail(index, row) {
         querySpecByProductCode({productCode:row.productCode}).then(res => {
        if (res.code === 200) {
          this.productSpes = res.data;
        }
      })
      this.orderdetail = row;
      this.allocDialogVisible = true;
         
      },
      handleComplate(index, row) {
      this.$confirm('是否要确认完成订单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            complateOrder(row.orderCode).then(response => {
              this.$message({
                message: '提交成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
        
        })
         
      },
           handleCancel(index, row) {
      this.$confirm('是否要确认完成订单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            cancelOrder(row.orderCode).then(response => {
              this.$message({
                message: '提交成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
        
        })
         
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            createUser(this.admin).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            createUser(this.admin).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.options=[]
              this.admin={}
              this.getList();
            })
          }
        })
      },
      handleAllocDialogConfirm(){
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params ={};
          params.userId = this.allocAdminId
          params.roleCode = this.allocRoleIds
          allocRole(params).then(response => {
            this.$message({
              message: '分配成功！',
              type: 'success'
            });
            this.allocDialogVisible = false;
          })
        })
      },
     
      
      getList() {
        debugger
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      },
      getAllRoleList() {
        fetchAllRoleList().then(response => {
          this.allRoleList = response.data;
        });
      },
        remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          setTimeout(() => {
            this.loading = false;
            var param ={};
            param.username = query
            getUserByUsernameFuzzy(param).then(response=>{
              this.options=response.data;
            })
          }, 200);
        } else {
          this.options = [];
        }
      },
      // getRoleByUserId(userId) {
      //   getRoleByAdmin(userId).then(response => {
      //     let allocRoleList = response.data;
    
      //   });
      // }
    }
  }
</script>
<style></style>


